package com.hd.assistant.dal.dao.ibatis.share;


import java.util.List;

import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.query.share.ShareQuery;
import com.hd.assistant.dal.dao.share.ShareDAO;
import com.hd.assistant.dal.dataobject.share.Share;
import com.taobao.common.dao.persistence.DBRoute;
import com.taobao.common.dao.persistence.SqlMapBaseDAO;


public class IbatisShareDao extends SqlMapBaseDAO implements ShareDAO {

	@Override
	public void insert(Share share) throws DAOException {
		if(share == null)
			throw new DAOException("param share == null");
		
		try {
			this.executeInsert("Share.insertShareDO", share,DBRoute.getAssistantRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisShareDao.insert error ",e);
			
		}
	}

	@Override
	public Share getShareById(Long id) throws DAOException {
		if(id == null)
			throw new DAOException("param id == null");
		try {
			return (Share)this.executeQueryForObject("Share.getShareById", id, DBRoute.getAssistantRoute());
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisShareDao.getShareById error ",e);
		}

	}

	@Override
	public ShareQuery getSharesByShareQuery(ShareQuery query)
			throws DAOException {
		if(query == null || query.getUserId() == null)
			throw new DAOException("param query == null || query.getUserId() == null");
		try {
			Long totalCount = (Long)this.executeQueryForObject("Share.selectCountsByQuery", query,  DBRoute.getAssistantRoute());
			query.setTotalCount(totalCount.intValue());
			
			@SuppressWarnings("unchecked")
			List<Share> shares = (List<Share>)this.executeQueryForList("Share.getShareByUserIdAndQuery", query,  DBRoute.getAssistantRoute());
			query.setShares(shares);
			return query;
		} catch (com.taobao.common.dao.persistence.exception.DAOException e) {
			throw new DAOException("IbatisShareDao.getSharesByShareQuery error ",e);
		}
	}

}
